home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CD Concept 6
/
CD Concept 06.iso
/
mac
/
UTILITAIRE
/
RLaB
/
rlib
/
trace.r
< prev
next >
Wrap
Text File
|
1994-09-23
|
524b
|
27 lines
//-------------------------------------------------------------------//
// Syntax: trace ( A )
// Description:
// Compute the trace (the sum of the diagonal elements) for the input
// matrix. `trace(A)' is the same as `sum( diag( A ) )'.
//-------------------------------------------------------------------//
trace = function(m)
{
if(m.class != "num")
{
error("must provide NUMERICAL input to trace()");
}
tr = 0;
for(i in 1:min( [m.nr, m.nc] ))
{
tr = tr + m[i;i];
}
return tr;
};